
 <!DOCTYPE HTML>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  
    <title>玩转Angular1(12)--创建日期选择组件 | Here. There.</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3, minimum-scale=1">
    
    <meta name="author" content="被删">
    
    <meta name="description" content="AngularJS(v1.5.8)已经成为项目们的基本框架，《玩转Angular1》系列用于记录项目中的一些好玩或者比较特别的思路。上一节我们讲了一些组件的设计思想，这里我们就按照其中的一些思想，创建一个日期的组件。">
    
    
    
    
    <link rel="alternate" href="/atom.xml" title="Here. There." type="application/atom+xml">
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/pacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/img/pacman.jpg">
    
    <link rel="stylesheet" href="/css/style.css">
    
<script type="text/javascript">
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?3d902de4a19cf2bf179534ffd2dd7b7f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</head>

  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/sun.png" alt="Here. There." title="Here. There."/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="Here. There.">Here. There.</a></h1>
				<h2 class="blog-motto">Love ice cream. Love sunshine. Love life. Love the world. Love myself. Love you.</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="菜单">
			</a></div>
			<nav class="animated">
				<ul>
					
						<li><a href="/">首页</a></li>
					
						<li><a href="https://github.com/godbasin/godbasin.github.io">所有文章</a></li>
					
						<li><a href="/archives">归档</a></li>
					
						<li><a href="/categories">分类</a></li>
					
						<li><a href="https://godbasin.github.io/front-end-playground">前端游乐场</a></li>
					
						<li><a href="/about">关于我</a></li>
					
				</ul>
			</nav>			
</div>

    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2017/03/12/angular-free-12-create-date-select-directive/" title="玩转Angular1(12)--创建日期选择组件" itemprop="url">玩转Angular1(12)--创建日期选择组件</a>
  </h1>
  <p class="article-author">By
    
      <a href="https://godbasin.github.io" title="被删">被删</a>
    </p>
  <p class="article-time">
    <time datetime="2017-03-12T03:02:57.000Z" itemprop="datePublished">2017-03-12</time>
    更新日期:<time datetime="2018-12-18T15:26:32.397Z" itemprop="dateModified">2018-12-18</time>
    
  </p>
</header>
	<div class="article-content">
		
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">文章目录</strong>
		<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#单日期组件"><span class="toc-number">1.</span> <span class="toc-text">单日期组件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#基本思路"><span class="toc-number">1.1.</span> <span class="toc-text">基本思路</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#selectDate-derective-ts"><span class="toc-number">1.2.</span> <span class="toc-text">selectDate.derective.ts</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#selectDate-template-html"><span class="toc-number">1.3.</span> <span class="toc-text">selectDate.template.html</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#双日期（时间）组件"><span class="toc-number">2.</span> <span class="toc-text">双日期（时间）组件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#基本思路-1"><span class="toc-number">2.1.</span> <span class="toc-text">基本思路</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#selectDateInterval-directive-ts"><span class="toc-number">2.2.</span> <span class="toc-text">selectDateInterval.directive.ts</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#日期组件的使用和展示"><span class="toc-number">2.3.</span> <span class="toc-text">日期组件的使用和展示</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束语"><span class="toc-number">3.</span> <span class="toc-text">结束语</span></a></li></ol>
		</div>
		
		<p>AngularJS(v1.5.8)已经成为项目们的基本框架，《玩转Angular1》系列用于记录项目中的一些好玩或者比较特别的思路。<br>上一节我们讲了一些组件的设计思想，这里我们就按照其中的一些思想，创建一个日期的组件。<br><a id="more"></a></p>
<h2 id="单日期组件"><a href="#单日期组件" class="headerlink" title="单日期组件"></a>单日期组件</h2><hr>
<h3 id="基本思路"><a href="#基本思路" class="headerlink" title="基本思路"></a>基本思路</h3><p>作为一个单日期组件，需要支持以下功能：</p>
<ul>
<li>维护最早可选日期、最晚可选日期，这些数据可初始化传入</li>
<li>可下拉展示、失焦关闭</li>
<li>可翻，前后一个月、前后一年</li>
<li>可选中日期，该数据可初始化传入</li>
<li>日历需显示星期几，则需要处理当月第一天为星期几</li>
<li>显示当月天数</li>
</ul>
<p>关于失焦处理，前面也提及过通过组件进行处理，请移步<a href="https://godbasin.github.io/2017/03/10/angular-free-10-directive-to-add-event-listener/">《玩转Angular1(10)–使用Directive指令来添加事件监听》</a>。<br>至于其他，我们遵循上一节<a href="https://godbasin.github.io/2017/03/11/angular-free-11-how-to-be-a-directive/">《玩转Angular1(11)–组件的自我修养》</a>讲到的一些组件设计的思想：</p>
<ul>
<li>日期组件对外提供最少状态数据，包括：最早可选日期、最晚可选日期、选中日期，未选择时显示内容（可选），选择后回调（可选）</li>
<li>日期组件维护自身的事件，包括：下拉展示、失焦关闭、前后一个月、前后一年、选中日期</li>
<li>日期组件对外提供选中回调，并传入选中日期</li>
</ul>
<h3 id="selectDate-derective-ts"><a href="#selectDate-derective-ts" class="headerlink" title="selectDate.derective.ts"></a>selectDate.derective.ts</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// app/shared/components/selectDate.derective.ts</span></div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> (ngModule) =&gt; &#123;</div><div class="line">    <span class="comment">/*</span></div><div class="line">    * @ 日期筛选组件</div><div class="line">    * @param: &#123;</div><div class="line">    *     data: &#123;</div><div class="line">    *       beginDate: string?, // 开始日期</div><div class="line">    *       endDate: string?,  // 结束日期</div><div class="line">    *       chosenDate: string?, // 选中日期</div><div class="line">    *     &#125;,</div><div class="line">    *     text: string, // 未选择日期时显示</div><div class="line">    *     chosen: function, // 选中日期后回调（传入选中日期：xxxx-xx-xx）</div><div class="line">    * &#125;</div><div class="line">    *</div><div class="line">    */</div><div class="line">    ngModule.directive(<span class="string">'selectDate'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</div><div class="line">        <span class="keyword">return</span> &#123;</div><div class="line">            <span class="attr">restrict</span>: <span class="string">'AE'</span>,</div><div class="line">            <span class="attr">scope</span>: &#123;</div><div class="line">                <span class="attr">data</span>: <span class="string">'=?'</span>,</div><div class="line">                <span class="attr">chosen</span>: <span class="string">'=?'</span>,</div><div class="line">                <span class="attr">text</span>: <span class="string">'@?'</span></div><div class="line">            &#125;,</div><div class="line">            <span class="attr">templateUrl</span>: <span class="string">'./shared/components/selectDate.template.html'</span>,</div><div class="line">            <span class="attr">transclude</span>: <span class="literal">true</span>,</div><div class="line">            <span class="attr">replace</span>: <span class="literal">false</span>,</div><div class="line">            link(scope, element, attrs) &#123;</div><div class="line">                <span class="keyword">const</span> dateNow = <span class="keyword">new</span> <span class="built_in">Date</span>();</div><div class="line">                <span class="keyword">const</span> todate = <span class="string">`<span class="subst">$&#123;dateNow.getFullYear()&#125;</span>-<span class="subst">$&#123;dateNow.getMonth() + <span class="number">1</span>&#125;</span>-<span class="subst">$&#123;dateNow.getDate()&#125;</span>`</span>;</div><div class="line">                <span class="keyword">let</span> beginDate;</div><div class="line">                <span class="keyword">let</span> endDate;</div><div class="line">                scope.isShown = <span class="literal">false</span>;</div><div class="line">                scope.toggle = toggle;</div><div class="line">                scope.init = init;</div><div class="line">                scope.chooseDay = chooseDay;</div><div class="line">                scope.lastMonth = lastMonth;</div><div class="line">                scope.nextMonth = nextMonth;</div><div class="line">                scope.lastYear = lastYear;</div><div class="line">                scope.nextYear = nextYear;</div><div class="line">                scope.isDayChosen = isDayChosen;</div><div class="line">                scope.isDayActive = isDayActive;</div><div class="line"></div><div class="line">                <span class="comment">// 下拉/收起日期组件</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">toggle</span>(<span class="params"></span>) </span>&#123;</div><div class="line">                    scope.isShown = !scope.isShown;</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 初始化事件</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">init</span>(<span class="params"></span>) </span>&#123;</div><div class="line">                    <span class="keyword">if</span>(!scope.data) &#123;scope.data = &#123;&#125;;&#125;</div><div class="line">                    scope.chosenDate = scope.data &amp;&amp; scope.data.chosenDate || todate;</div><div class="line">                    beginDate = scope.data &amp;&amp; scope.data.beginDate || <span class="string">'1700-01-01'</span>;</div><div class="line">                    endDate = scope.data &amp;&amp; scope.data.endDate || <span class="string">'2900-12-30'</span>;</div><div class="line">                    render();</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 刷新日历事件</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">date?</span>) </span>&#123;</div><div class="line">                    scope.readingDate = FormatDate(date || scope.data.chosenDate || todate);</div><div class="line">                    <span class="keyword">const</span> year = <span class="built_in">Number</span>(scope.readingDate.split(<span class="string">'-'</span>)[<span class="number">0</span>]);</div><div class="line">                    <span class="keyword">const</span> month = <span class="built_in">Number</span>(scope.readingDate.split(<span class="string">'-'</span>)[<span class="number">1</span>]);</div><div class="line">                    <span class="keyword">const</span> firstday = getFirstDay(year, month);</div><div class="line">                    <span class="keyword">const</span> monthlength = getMonthLength(year, month);</div><div class="line">                    <span class="comment">// readingDays保存当前月份的日期数组</span></div><div class="line">                    scope.readingDays = [];</div><div class="line">                    <span class="comment">// 传入''则为空，用于星期对齐</span></div><div class="line">                    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; firstday; i++) &#123;</div><div class="line">                        scope.readingDays.push(<span class="string">''</span>);</div><div class="line">                    &#125;</div><div class="line">                    <span class="comment">// 传入当前月份日期</span></div><div class="line">                    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">1</span>; i &lt;= monthlength; i++) &#123;</div><div class="line">                        scope.readingDays.push(i);</div><div class="line">                    &#125;</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 选中日期事件</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">chooseDay</span>(<span class="params">day</span>)</span>&#123;</div><div class="line">                    <span class="keyword">const</span> &#123;year, month&#125; = getDateDetail(scope.readingDate);</div><div class="line">                    scope.chosenDate = scope.data.chosenDate = <span class="string">`<span class="subst">$&#123;year&#125;</span>-<span class="subst">$&#123;month&#125;</span>-<span class="subst">$&#123;day&#125;</span>`</span>;</div><div class="line">                    scope.isShown = <span class="literal">false</span>;</div><div class="line">                    <span class="keyword">if</span> (<span class="keyword">typeof</span> scope.chosen === <span class="string">'function'</span>) &#123;</div><div class="line">                        scope.chosen(scope.chosenDate);</div><div class="line">                    &#125;</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 选择上一个月份日期</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">lastMonth</span>(<span class="params"></span>) </span>&#123;</div><div class="line">                    <span class="keyword">let</span> &#123;year, month, day&#125; = getDateDetail(scope.readingDate);</div><div class="line">                    <span class="keyword">if</span> (--month === <span class="number">0</span>) &#123;</div><div class="line">                        render(<span class="string">`<span class="subst">$&#123;year - <span class="number">1</span>&#125;</span>-12-<span class="subst">$&#123;day&#125;</span>`</span>);</div><div class="line">                    &#125; <span class="keyword">else</span> &#123;</div><div class="line">                        render(<span class="string">`<span class="subst">$&#123;year&#125;</span>-<span class="subst">$&#123;month&#125;</span>-<span class="subst">$&#123;day&#125;</span>`</span>);</div><div class="line">                    &#125;</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 选择下一个月份日期</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">nextMonth</span>(<span class="params"></span>) </span>&#123;</div><div class="line">                    <span class="keyword">let</span> &#123;year, month, day&#125; = getDateDetail(scope.readingDate);</div><div class="line">                    <span class="keyword">if</span> (++month &gt; <span class="number">12</span>) &#123;</div><div class="line">                        render(<span class="string">`<span class="subst">$&#123;year + <span class="number">1</span>&#125;</span>-1-<span class="subst">$&#123;day&#125;</span>`</span>);</div><div class="line">                    &#125; <span class="keyword">else</span> &#123;</div><div class="line">                        render(<span class="string">`<span class="subst">$&#123;year&#125;</span>-<span class="subst">$&#123;month&#125;</span>-<span class="subst">$&#123;day&#125;</span>`</span>);</div><div class="line">                    &#125;</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 选择上一年日期</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">lastYear</span>(<span class="params"></span>) </span>&#123;</div><div class="line">                    <span class="keyword">let</span> &#123;year, month, day&#125; = getDateDetail(scope.readingDate);</div><div class="line">                    render(<span class="string">`<span class="subst">$&#123;year - <span class="number">1</span>&#125;</span>-<span class="subst">$&#123;month&#125;</span>-<span class="subst">$&#123;day&#125;</span>`</span>);</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 选择下一年日期</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">nextYear</span>(<span class="params"></span>) </span>&#123;</div><div class="line">                    <span class="keyword">let</span> &#123;year, month, day&#125; = getDateDetail(scope.readingDate);</div><div class="line">                    render(<span class="string">`<span class="subst">$&#123;year + <span class="number">1</span>&#125;</span>-<span class="subst">$&#123;month&#125;</span>-<span class="subst">$&#123;day&#125;</span>`</span>);</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 判断日期是否当前选中日期</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">isDayChosen</span>(<span class="params">day</span>) </span>&#123;</div><div class="line">                    <span class="keyword">if</span> (!day) &#123; <span class="keyword">return</span> <span class="literal">false</span>; &#125;</div><div class="line">                    <span class="keyword">const</span> &#123;year, month&#125; = getDateDetail(scope.readingDate);</div><div class="line">                    <span class="keyword">return</span> FormatDate(<span class="string">`<span class="subst">$&#123;year&#125;</span>-<span class="subst">$&#123;month&#125;</span>-<span class="subst">$&#123;day&#125;</span>`</span>) === FormatDate(scope.chosenDate);</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 判断当前日期是否有效日期</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">isDayActive</span>(<span class="params">day</span>) </span>&#123;</div><div class="line">                    <span class="keyword">if</span> (!day) &#123; <span class="keyword">return</span> <span class="literal">false</span>; &#125;</div><div class="line">                    <span class="keyword">const</span> &#123;year, month&#125; = getDateDetail(scope.readingDate);</div><div class="line">                    <span class="keyword">const</span> date = FormatDate(<span class="string">`<span class="subst">$&#123;year&#125;</span>-<span class="subst">$&#123;month&#125;</span>-<span class="subst">$&#123;day&#125;</span>`</span>);</div><div class="line">                    <span class="keyword">return</span> (date &gt;= FormatDate(beginDate) &amp;&amp; date &lt;= FormatDate(endDate));</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 获取当月第一天星期几</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">getFirstDay</span>(<span class="params">year, month</span>) </span>&#123;</div><div class="line">                    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Date</span>(year, month - <span class="number">1</span>, <span class="number">1</span>).getDay();</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 获取当月天数</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">getMonthLength</span>(<span class="params">year, month</span>) </span>&#123;</div><div class="line">                    <span class="keyword">const</span> nextMonth = <span class="keyword">new</span> <span class="built_in">Date</span>(year, month, <span class="number">1</span>);</div><div class="line">                    nextMonth.setHours(nextMonth.getHours() - <span class="number">2</span>);</div><div class="line">                    <span class="keyword">return</span> nextMonth.getDate();</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 获取当前查看日期的年月日</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">getDateDetail</span>(<span class="params">date</span>) </span>&#123;</div><div class="line">                    <span class="keyword">let</span> year = <span class="built_in">Number</span>(scope.readingDate.split(<span class="string">'-'</span>)[<span class="number">0</span>]);</div><div class="line">                    <span class="keyword">let</span> month = <span class="built_in">Number</span>(scope.readingDate.split(<span class="string">'-'</span>)[<span class="number">1</span>]);</div><div class="line">                    <span class="keyword">let</span> day = <span class="built_in">Number</span>(scope.readingDate.split(<span class="string">'-'</span>)[<span class="number">2</span>]);</div><div class="line">                    <span class="keyword">return</span> &#123; year, month, day &#125;;</div><div class="line">                &#125;</div><div class="line">            &#125;,</div><div class="line">        &#125;;</div><div class="line">    &#125;);</div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<h3 id="selectDate-template-html"><a href="#selectDate-template-html" class="headerlink" title="selectDate.template.html"></a>selectDate.template.html</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!--app/shared/components/selectDate.template.html--&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">select</span>=<span class="string">"select"</span> <span class="attr">on-focus-lost</span>=<span class="string">"isShown = false"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">p</span> <span class="attr">ng-click</span>=<span class="string">"init();toggle();"</span> <span class="attr">ng-class</span>=<span class="string">"isShown ? 'active' : ''"</span>&gt;</span><span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">"btn-calendar-icon"</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span>&#123;&#123; (data &amp;&amp; data.chosenDate | date) || text || '请选择日期' &#125;&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">aside</span> <span class="attr">class</span>=<span class="string">"calendar"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">header</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;&#123; readingDate.split('-')[0] &#125;&#125;年<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"next-year"</span> <span class="attr">ng-click</span>=<span class="string">"nextYear()"</span>&gt;</span><span class="tag">&lt;<span class="name">i</span> <span class="attr">class</span>=<span class="string">"fa fa-angle-double-right"</span>&gt;</span><span class="tag">&lt;/<span class="name">i</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"next-month"</span> <span class="attr">ng-click</span>=<span class="string">"nextMonth()"</span>&gt;</span><span class="tag">&lt;<span class="name">i</span> <span class="attr">class</span>=<span class="string">"fa fa-angle-right"</span>&gt;</span><span class="tag">&lt;/<span class="name">i</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">            <span class="comment">&lt;!-- &lt;a class="today" ng-click="render(yC,mC,dC)"&gt;&lt;/a&gt; --&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">p</span> <span class="attr">class</span>=<span class="string">"month"</span>&gt;</span>&#123;&#123; readingDate.split('-')[1] | number &#125;&#125;月<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"last-month"</span> <span class="attr">ng-click</span>=<span class="string">"lastMonth()"</span>&gt;</span><span class="tag">&lt;<span class="name">i</span> <span class="attr">class</span>=<span class="string">"fa fa-angle-left"</span>&gt;</span><span class="tag">&lt;/<span class="name">i</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"last-year"</span> <span class="attr">ng-click</span>=<span class="string">"lastYear()"</span>&gt;</span><span class="tag">&lt;<span class="name">i</span> <span class="attr">class</span>=<span class="string">"fa fa-angle-double-left"</span>&gt;</span><span class="tag">&lt;/<span class="name">i</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">header</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">section</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">aside</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">p</span>&gt;</span>日<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">p</span>&gt;</span>一<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">p</span>&gt;</span>二<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">p</span>&gt;</span>三<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">p</span>&gt;</span>四<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">p</span>&gt;</span>五<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">p</span>&gt;</span>六<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">            <span class="tag">&lt;/<span class="name">aside</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">article</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">p</span> <span class="attr">ng-repeat</span>=<span class="string">"day in readingDays"</span> <span class="attr">ng-class</span>=<span class="string">"isDayChosen(day) ? 'chosen' : isDayActive(day) ? 'active' : ''"</span> <span class="attr">ng-click</span>=<span class="string">"(isDayActive(day) || isDayChosen(day)) ? chooseDay(day) : ''"</span>&gt;</span>&#123;&#123; day &#125;&#125;<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">            <span class="tag">&lt;/<span class="name">article</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">section</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">aside</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<p>其中，<code>[on-focus-lost]</code>组件则前面我们说到的，用于失焦处理。</p>
<h2 id="双日期（时间）组件"><a href="#双日期（时间）组件" class="headerlink" title="双日期（时间）组件"></a>双日期（时间）组件</h2><hr>
<h3 id="基本思路-1"><a href="#基本思路-1" class="headerlink" title="基本思路"></a>基本思路</h3><p>有些时候，我们的日期是成对出现的，即选择一个时间段，此时我们需要维护两个日期组件间的关系：</p>
<ul>
<li>开始日期的选择，将影响结束日期的最早可选时间</li>
<li>结束日期的选择，将影响开始日期的最晚可选时间</li>
</ul>
<p>作为一个双日期组件，需要支持以下功能：</p>
<ul>
<li>维护最早可选日期、最晚可选日期</li>
<li>基本的单个日期组件的功能</li>
<li>可选中开始日期和结束日期，该数据可初始化传入</li>
<li>开始日期和结束日期的约束关系</li>
</ul>
<p>而很多时候，我们的日期和时间也是匹配出现的，此时则为<code>[开始日期][开始时间]-[结束日期][结束时间]</code>的方式出现。</p>
<p>我们涉及的一些组件设计的思想：</p>
<ul>
<li>日期组件对外提供最少状态数据，包括：最早可选日期、最晚可选日期、选中日期（开始、结束），未选择时显示内容（开始、结束）（可选）</li>
<li>日期组件维护自身的事件</li>
<li>日期组件对外提供选中回调，并传入选中日期或者时间</li>
<li>日期组件通过配置选项，来确定是否显示时间</li>
</ul>
<p>这里的时间组件，也是一个封装好的组件，我们在这不进行详细说明了，有兴趣的小伙伴可以去看看。</p>
<h3 id="selectDateInterval-directive-ts"><a href="#selectDateInterval-directive-ts" class="headerlink" title="selectDateInterval.directive.ts"></a>selectDateInterval.directive.ts</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> (ngModule) =&gt; &#123;</div><div class="line">    <span class="comment">/*</span></div><div class="line">    * @ 日期（时间）间断筛选组件 </div><div class="line">    * @param: &#123;</div><div class="line">    *     data: &#123;</div><div class="line">    *       beginDate: string?, // 开始日期</div><div class="line">    *       endDate: string?,  // 结束日期</div><div class="line">    *       chosenBeginDate: string?, // 选中开始日期</div><div class="line">    *       chosenEndDate: string?, // 选中结束日期</div><div class="line">    *       beginTime: string? // 开始时间</div><div class="line">    *       endTime: string? // 结束时间</div><div class="line">    *     &#125;,</div><div class="line">    *     beginText: string?, // 未选择开始日期时显示</div><div class="line">    *     endText: string?, // 未选择结束日期时显示</div><div class="line">    *     withTime: string? // 是否有时间控件,传入任何值均为是</div><div class="line">    *     chosen: function, // 选中日期后回调（传入选中数据：[data, dataType], dataType有四种：'beginData', 'endData', 'beginTime', 'endTime'）</div><div class="line">    * &#125;</div><div class="line">    *</div><div class="line">    */</div><div class="line">    ngModule.directive(<span class="string">'selectDateInterval'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</div><div class="line">        <span class="keyword">return</span> &#123;</div><div class="line">            <span class="attr">restrict</span>: <span class="string">'AE'</span>,</div><div class="line">            <span class="attr">scope</span>: &#123;</div><div class="line">                <span class="attr">data</span>: <span class="string">'=?'</span>,</div><div class="line">                <span class="attr">chosen</span>: <span class="string">'=?'</span>,</div><div class="line">                <span class="attr">beginText</span>: <span class="string">'@?'</span>,</div><div class="line">                <span class="attr">endText</span>: <span class="string">'@?'</span>,</div><div class="line">                <span class="attr">withTime</span>: <span class="string">'@?'</span>,</div><div class="line">            &#125;,</div><div class="line">            <span class="attr">template</span>: <span class="string">`</span></div><div class="line">            &lt;div&gt;</div><div class="line">                &lt;div select-date data="beginData" class="inline-block" text="&#123;&#123; data &amp;&amp; data.chosenBeginDate || beginText || '开始日期' &#125;&#125;" chosen="chooseBeginDate"&gt;&lt;/div&gt;</div><div class="line">                &lt;div ng-if="withTime" select-time class="inline-block" type="begin" chosen="chooseTime" selected="beginTime"&gt;&lt;/div&gt;</div><div class="line">                &lt;span class="bold-vertical-line inline-block"&gt;&lt;/span&gt;</div><div class="line">                &lt;div select-date data="endData" class="inline-block" text="&#123;&#123; data &amp;&amp; data.chosenEndDate || endText || '结束日期' &#125;&#125;" chosen="chooseEndDate"&gt;&lt;/div&gt;</div><div class="line">                &lt;div ng-if="withTime" select-time class="inline-block" type="end" chosen="chooseTime" selected="endTime"&gt;&lt;/div&gt;</div><div class="line">            &lt;/div&gt;</div><div class="line">            `,</div><div class="line">            <span class="attr">transclude</span>: <span class="literal">true</span>,</div><div class="line">            <span class="attr">replace</span>: <span class="literal">true</span>,</div><div class="line">            link(scope, element, attrs) &#123;</div><div class="line">                <span class="keyword">const</span> dateNow = <span class="keyword">new</span> <span class="built_in">Date</span>();</div><div class="line">                <span class="keyword">const</span> todate = <span class="string">`<span class="subst">$&#123;dateNow.getFullYear()&#125;</span>-<span class="subst">$&#123;dateNow.getMonth() + <span class="number">1</span>&#125;</span>-<span class="subst">$&#123;dateNow.getDate()&#125;</span>`</span>;</div><div class="line">                scope.data = &#123;&#125;;</div><div class="line">                scope.beginData = &#123;&#125;;</div><div class="line">                scope.endData = &#123;&#125;;</div><div class="line">                scope.init = init;</div><div class="line">                scope.chooseBeginDate = chooseBeginDate;</div><div class="line">                scope.chooseEndDate = chooseEndDate;</div><div class="line">                scope.chooseTime = chooseTime;</div><div class="line"></div><div class="line">                <span class="comment">// 初始化事件</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">init</span>(<span class="params"></span>) </span>&#123;</div><div class="line">                    scope.withTime = !!scope.withTime;</div><div class="line">                    scope.beginData = &#123;</div><div class="line">                        <span class="attr">beginDate</span>: scope.data &amp;&amp; scope.data.beginDate,</div><div class="line">                        <span class="attr">endDate</span>: scope.data &amp;&amp; scope.data.endDate,</div><div class="line">                        <span class="attr">chosenDate</span>: scope.data &amp;&amp; scope.data.chosenBeginDate</div><div class="line">                    &#125;;</div><div class="line">                    scope.endData = &#123;</div><div class="line">                        <span class="attr">beginDate</span>: scope.data &amp;&amp; scope.data.beginDate,</div><div class="line">                        <span class="attr">endDate</span>: scope.data &amp;&amp; scope.data.endDate,</div><div class="line">                        <span class="attr">chosenDate</span>: scope.data &amp;&amp; scope.data.chosenEndDate</div><div class="line">                    &#125;</div><div class="line">                    <span class="keyword">if</span>(scope.withTime)&#123;</div><div class="line">                        scope.beginTime = scope.data &amp;&amp; scope.data.beginTime || <span class="string">'00:00:00'</span>;</div><div class="line">                        scope.endTime = scope.data &amp;&amp; scope.data.endTime || <span class="string">'23:59:59'</span>;</div><div class="line">                    &#125;</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 选择开始日期事件</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">chooseBeginDate</span>(<span class="params">date</span>) </span>&#123;</div><div class="line">                    scope.data.chosenBeginDate = date;</div><div class="line">                    scope.endData = &#123;...scope.endData, <span class="attr">beginDate</span>: date&#125;;</div><div class="line">                    <span class="keyword">if</span> (<span class="keyword">typeof</span> scope.chosen === <span class="string">'function'</span>) &#123;</div><div class="line">                        scope.chosen(date, <span class="string">'beginDate'</span>);</div><div class="line">                    &#125;</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 选择结束日期事件</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">chooseEndDate</span>(<span class="params">date</span>) </span>&#123;</div><div class="line">                    scope.data.chosenEndDate = date;</div><div class="line">                    scope.beginData = &#123;...scope.beginData, <span class="attr">endDate</span>: date&#125;;</div><div class="line">                    <span class="keyword">if</span> (<span class="keyword">typeof</span> scope.chosen === <span class="string">'function'</span>) &#123;</div><div class="line">                        scope.chosen(date, <span class="string">'endDate'</span>);</div><div class="line">                    &#125;</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 选择时间事件</span></div><div class="line">                <span class="function"><span class="keyword">function</span> <span class="title">chooseTime</span>(<span class="params">time, type</span>) </span>&#123;</div><div class="line">                    <span class="keyword">if</span>(type === <span class="string">'begin'</span>)&#123;</div><div class="line">                        scope.data.beginTime = scope.beginTime = time;</div><div class="line">                    &#125;<span class="keyword">else</span> <span class="keyword">if</span>(type === <span class="string">'end'</span>)&#123;</div><div class="line">                        scope.data.endTime = scope.endTime = time;</div><div class="line">                    &#125;</div><div class="line">                    <span class="keyword">if</span> (<span class="keyword">typeof</span> scope.chosen === <span class="string">'function'</span>) &#123;</div><div class="line">                        scope.chosen(time, <span class="string">`<span class="subst">$&#123;type&#125;</span>Time`</span>);</div><div class="line">                    &#125;</div><div class="line">                &#125;</div><div class="line"></div><div class="line">                <span class="comment">// 监听data进行初始化</span></div><div class="line">                scope.$watch(<span class="string">'data'</span>, (n,o)=&gt;&#123;</div><div class="line">                    <span class="keyword">if</span>(n !== <span class="literal">undefined</span>)&#123;</div><div class="line">                        scope.data = n;</div><div class="line">                        init();</div><div class="line">                    &#125;</div><div class="line">                &#125;)</div><div class="line">            &#125;,</div><div class="line">        &#125;;</div><div class="line">    &#125;);</div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<h3 id="日期组件的使用和展示"><a href="#日期组件的使用和展示" class="headerlink" title="日期组件的使用和展示"></a>日期组件的使用和展示</h3><p>我们将在账户管理模块，简单地展示一下我们的日期组件，当然我们需要添加控制器：</p>
<ul>
<li>account.controller.ts</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// app/modules/home/account/account.controller.ts</span></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">AccountCtrl</span> </span>&#123;</div><div class="line">    <span class="comment">// 获取依赖</span></div><div class="line">    public <span class="keyword">static</span> $inject = [</div><div class="line">        <span class="string">'$scope'</span>,</div><div class="line">    ];</div><div class="line"></div><div class="line">    <span class="comment">// 注入依赖</span></div><div class="line">    <span class="keyword">constructor</span>(</div><div class="line">        private $scope</div><div class="line">    ) &#123;</div><div class="line">        <span class="comment">// VM用于绑定模板相关内容</span></div><div class="line">        $scope.VM = <span class="keyword">this</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    chosen(date)&#123;Notify(&#123;<span class="attr">title</span>: <span class="string">`选中<span class="subst">$&#123;date&#125;</span>`</span>&#125;);&#125;</div><div class="line">    chosenInterval(date, dateType)&#123;Notify(&#123;<span class="attr">title</span>: <span class="string">`选中<span class="subst">$&#123;dateType&#125;</span>，为<span class="subst">$&#123;date&#125;</span>`</span>&#125;);&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> (ngModule) =&gt; &#123;</div><div class="line">    ngModule.controller(<span class="string">'AccountCtrl'</span>, AccountCtrl);</div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<ul>
<li>account.template.html</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>单日期</div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">select-date</span> <span class="attr">chosen</span>=<span class="string">"VM.chosen"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>双日期</div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">select-date-interval</span> <span class="attr">chosen</span>=<span class="string">"VM.chosenInterval"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>双日期带时间</div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">select-date-interval</span> <span class="attr">with-time</span>=<span class="string">"yes"</span> <span class="attr">chosen</span>=<span class="string">"VM.chosenInterval"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1485179158%281%29.png" alt="image"></p>
<h2 id="结束语"><a href="#结束语" class="headerlink" title="结束语"></a>结束语</h2><hr>
<p>这节主要简单日期组件的创建，以及一些设计思想。其实把组件做成可配置，这样的想法也是很棒的，多亏了团队的小朋友们，发散的思维给大家都带来了很棒的收获呢。<br><a href="https://github.com/godbasin/godbasin.github.io/tree/blog-codes/angular-free/12-create-date-select-directive" target="_blank" rel="external">此处查看项目代码</a><br><a href="http://angular-free.godbasin.com/angular-free-12-create-date-select-directive/index.html" target="_blank" rel="external">此处查看页面效果</a></p>
  
		
		<p style="margin-top:50px;">
			查看Github有更多内容噢：<a href="https://github.com/godbasin">https://github.com/godbasin</a>
			<br>
			更欢迎来<a href="https://godbasin.github.io/front-end-playground">被删的前端游乐场</a>边撸猫边学前端噢
		</p>
	</div>
	<img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" width="200" height="200" style="display:block;margin: 0 auto;" />
	<p style="text-align: center;margin-top: 10px;margin-bottom: 20px;">码生艰难，写文不易，给我家猪囤点猫粮了喵~</p>
	<div class="author-right">
  		<p>作者：被删</p>
  		<p>出处：<a href="https://godbasin.github.io">https://godbasin.github.io</a></p>
  		<p>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</p>
	</div>
		<footer class="article-footer clearfix">

  <div class="article-tags">
  
  <span></span> <a href="/tags/笔记/">笔记</a>
  </div>


<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/angular混搭/">angular混搭</a>
</div>



<div class="article-share" id="share">

  
<!-- JiaThis Button BEGIN -->
<div class="jiathis_style_24x24">
	<a class="jiathis_button_qzone"></a>
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_tqq"></a>
	<a class="jiathis_button_weixin"></a>
	<a class="jiathis_button_renren"></a>
	<a href="http://www.jiathis.com/share?uid=2134021" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
</div>
<script type="text/javascript">
var jiathis_config = {data_track_clickback:'true'};
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code_mini/jia.js?uid=2134021" charset="utf-8"></script>
<!-- JiaThis Button END -->   


</div>
</footer>   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2017/03/18/angular-free-13-service-work-with-directive/" title="玩转Angular1(13)--服务与指令的配合使用">
  <strong>PREVIOUS:</strong><br/>
  <span>
  玩转Angular1(13)--服务与指令的配合使用</span>
</a>
</div>


<div class="next">
<a href="/2017/03/11/angular-free-11-how-to-be-a-directive/"  title="玩转Angular1(11)--组件的自我修养">
 <strong>NEXT:</strong><br/> 
 <span>玩转Angular1(11)--组件的自我修养
</span>
</a>
</div>

</nav>

	<!-- 如果不是首页且没使用 `comments: false` 关闭评论，则尝试加载评论 -->

    <!-- 配置中启用多说时，导入相应代码 -->
    


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="显示侧边栏"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">文章目录</strong>
  <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#单日期组件"><span class="toc-number">1.</span> <span class="toc-text">单日期组件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#基本思路"><span class="toc-number">1.1.</span> <span class="toc-text">基本思路</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#selectDate-derective-ts"><span class="toc-number">1.2.</span> <span class="toc-text">selectDate.derective.ts</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#selectDate-template-html"><span class="toc-number">1.3.</span> <span class="toc-text">selectDate.template.html</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#双日期（时间）组件"><span class="toc-number">2.</span> <span class="toc-text">双日期（时间）组件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#基本思路-1"><span class="toc-number">2.1.</span> <span class="toc-text">基本思路</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#selectDateInterval-directive-ts"><span class="toc-number">2.2.</span> <span class="toc-text">selectDateInterval.directive.ts</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#日期组件的使用和展示"><span class="toc-number">2.3.</span> <span class="toc-text">日期组件的使用和展示</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束语"><span class="toc-number">3.</span> <span class="toc-text">结束语</span></a></li></ol>
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="隐藏侧边栏"></a></div>
<aside class="clearfix">

  	
	<div class="archiveslist">
		<p class="asidetitle">最近文章</p>
			<ul class="archive-list">
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/11/15/wxapp-latest-20191115/" title="小程序开发月刊第11期（20191115）">小程序开发月刊第11期（201911...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/11/10/change-log/" title="前端 CHANGELOG 生成指南">前端 CHANGELOG 生成指南...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/11/09/wxapp-global-data-behavior/" title="小程序的奇技淫巧 -- globalDataBehavior管理全局状态">小程序的奇技淫巧 -- global...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/10/15/wxapp-latest-20191015/" title="小程序开发月刊第十期（20191015）">小程序开发月刊第十期（2019101...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/10/13/about-front-end-3-growth/" title="前端这几年--3.关于成长和焦虑">前端这几年--3.关于成长和焦虑...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/09/16/wxapp-latest-20190916/" title="小程序开发月刊第九期（20190916）">小程序开发月刊第九期（2019091...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/08/15/wxapp-latest-20190815/" title="小程序开发月刊第八期（20190815）">小程序开发月刊第八期（2019081...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/27/front-end-playground/" title="被删的前端游乐场建成！">被删的前端游乐场建成！</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/21/vue-for-everyone-3/" title="9102全员学Vue--3.把页面拼成个Web应用">9102全员学Vue--3.把页面拼...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/15/wxapp-latest-20190715/" title="小程序开发月刊第七期（20190715）">小程序开发月刊第七期（2019071...</a>
				</li>
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
			</ul>
	</div>


  
  <div class="archiveslist">
    <p class="asidetitle"><a href="/archives">归档</a></p>
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">十一月 2019</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">十月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">九月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">八月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">七月 2019</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">六月 2019</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/05/">五月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">四月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">三月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/02/">二月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/01/">一月 2019</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/12/">十二月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/11/">十一月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">十月 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/09/">九月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/08/">八月 2018</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/07/">七月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/06/">六月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">五月 2018</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/04/">四月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">三月 2018</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">二月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/">一月 2018</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">十二月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/11/">十一月 2017</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/10/">十月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/">九月 2017</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/08/">八月 2017</a><span class="archive-list-count">11</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/07/">七月 2017</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/06/">六月 2017</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/">五月 2017</a><span class="archive-list-count">15</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/04/">四月 2017</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/">三月 2017</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/02/">二月 2017</a><span class="archive-list-count">41</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/01/">一月 2017</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/12/">十二月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/11/">十一月 2016</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/10/">十月 2016</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/09/">九月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/08/">八月 2016</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/07/">七月 2016</a><span class="archive-list-count">14</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/06/">六月 2016</a><span class="archive-list-count">9</span></li></ul>
  </div>


  
<div class="archiveslist">
	<p class="asidetitle"><a href="/categories">分类</a></p>
		<ul class="archive-list">
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/CSS炒饭/" title="CSS炒饭">CSS炒饭<sup>3</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/D3小馒头/" title="D3小馒头">D3小馒头<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/angular2火锅/" title="angular2火锅">angular2火锅<sup>25</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/angular混搭/" title="angular混搭">angular混搭<sup>33</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/box2djs方糖/" title="box2djs方糖">box2djs方糖<sup>34</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/cyclejs哈根达斯/" title="cyclejs哈根达斯">cyclejs哈根达斯<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/jQuery杂烩/" title="jQuery杂烩">jQuery杂烩<sup>3</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/js什锦/" title="js什锦">js什锦<sup>26</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/react沙拉/" title="react沙拉">react沙拉<sup>16</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/three-js奶茶/" title="three.js奶茶">three.js奶茶<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/vue八宝粥/" title="vue八宝粥">vue八宝粥<sup>29</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/webpack宾治/" title="webpack宾治">webpack宾治<sup>9</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/web乱炖/" title="web乱炖">web乱炖<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/前端满汉全席/" title="前端满汉全席">前端满汉全席<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/喵/" title="喵">喵<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/小程序双皮奶/" title="小程序双皮奶">小程序双皮奶<sup>27</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/工作这杯茶/" title="工作这杯茶">工作这杯茶<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/应用巧克力盒/" title="应用巧克力盒">应用巧克力盒<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/思想棉花糖/" title="思想棉花糖">思想棉花糖<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/柴米油盐工具集/" title="柴米油盐工具集">柴米油盐工具集<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/自动化甜筒/" title="自动化甜筒">自动化甜筒<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/非前端钙片/" title="非前端钙片">非前端钙片<sup>7</sup></a>
			</li>
		
		</ul>
</div>


  
<div class="archiveslist">
	<p class="asidetitle">标签</p>
		<ul class="archive-list">
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/分享/" title="分享">分享<sup>44</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/原创部件/" title="原创部件">原创部件<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/心态/" title="心态">心态<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/教程/" title="教程">教程<sup>73</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/晒猫/" title="晒猫">晒猫<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/笔记/" title="笔记">笔记<sup>121</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/算法/" title="算法">算法<sup>9</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/自制插件/" title="自制插件">自制插件<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/逻辑实现/" title="逻辑实现">逻辑实现<sup>5</sup></a>
			</li>
		
		</ul>
</div>


    <div class="archiveslist">
    <p class="asidetitle"><a href="/archives">about</a></p>
      <ul class="archive-list">
      	<li class="archive-list-item">
      		<a>wangbeishan@163.com</a>
      		<a href="https://github.com/godbasin">github.com/godbasin</a>
      	</li>
      </ul>
  </div>

  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS 订阅</a>
</div>

</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<section class="info">
		<p> 即使梦想再小，却很伟大 ^_^ </p>
	</section>
	 
		<p class="copyright">Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/A-limon/pacman" target="_blank" title="Pacman">Pacman</a> © 2019 
		
		<a href="https://godbasin.github.io" target="_blank" title="被删">被删</a>
		
		</p>
</div>
</footer>
    <script src="/js/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>







  </body>
</html>
